package com.javawork.juc.one;

import java.util.concurrent.CompletableFuture;

/**
 * @author zhangxiaolu
 * @date 2023-02-05 21:54
 */
public class CompletableFutureApi3Demo {

    /**
     * thenRun(Runnable runnable) 任务A执行完执行B，B不需要A的结果
     * thenAccept(Consumer action) 任务A执行完执行B，B需要A的结果，B无返回值
     * thenApply(Function fn) 任务A执行完执行B，B需要A的结果，B有返回值
     *
     * @param args
     */
    public static void main(String[] args) {
        CompletableFuture.supplyAsync(() -> {
            return 1;
        }).thenApply(f -> {
            return f + 2;
        }).thenApply(f -> {
            return f + 3;
        }).thenAccept(System.out::println);

        System.out.println(CompletableFuture.supplyAsync(() -> "resultA").thenRun(() -> {
        }).join());
        System.out.println(CompletableFuture.supplyAsync(() -> "resultA").thenAccept(r -> {
            System.out.println(r);
        }).join());
        System.out.println(CompletableFuture.supplyAsync(() -> "resultA").thenApply(r -> r + "resultB").join());
    }
}
